package com.slot.slotprototype;

import android.app.Activity;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SlotPrototype extends Activity {
	/** Called when the activity is first created. */
	Animation[] a;
	ImageView[] img;
	TextView mtextview;
	int ireels = 5;
	int count_stop=0;
	boolean isPlaing = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main);
		
		int ht;
		int wt;
		DisplayMetrics displaymetrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
		ht = displaymetrics.heightPixels;
		wt = displaymetrics.widthPixels;

		mtextview = (TextView) findViewById(R.id.switcher);
		mtextview.setText("GAME OVER,\n PLACE YOUR BET");
		mtextview.setTextColor(Color.YELLOW);
		mtextview.setTextSize(12);
		mtextview.setGravity(Gravity.CENTER_HORIZONTAL);
		img = new ImageView[ireels];
		a = new Animation[ireels];
		int[] reelids = new int[] { R.id.reel0, R.id.reel1, R.id.reel2,
				R.id.reel3, R.id.reel4 };
		int[] reels_position=new int[]{(int)(wt*0.08),(int)(wt*0.26),(int)(wt*0.435),(int)(wt*0.61),(int)(wt*0.794)};
		for (int i = 0; i < ireels; i++) {
			a[i] = AnimationUtils.loadAnimation(this, R.anim.top_to_bottom);
			a[i].setDuration(2000 + i * 300);
			a[i].reset();
			img[i] = (ImageView) findViewById(reelids[i]);
			RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			lp.setMargins(reels_position[i],(int)(-4.23*ht),0,0);
			img[i].setLayoutParams(lp);
		}
	}

	public final boolean onTouchEvent(MotionEvent event) {
		if (event.getAction() == MotionEvent.ACTION_DOWN&& !isPlaing) {
			for (int i = 0; i < ireels; i++) {
				img[i].startAnimation(a[i]);
			}
			mtextview.setText("");
			MediaPlayer mp = MediaPlayer.create(getBaseContext(), R.raw.reels);
			mp.start();
			mp.setOnCompletionListener(new OnCompletionListener() {

				@Override
				public void onCompletion(MediaPlayer mp) {
					mp.release();
				}
			});
			isPlaing = true;
			count_stop=0;
			realstopdelay(1700);
			realstopdelay(2000);
			realstopdelay(2300);
			realstopdelay(2600);
			realstopdelay(2900);
			delay(3500);
			return true;
		}
		return super.onTouchEvent(event);

	}
	private void delay(int i) {
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {
			public void run() {
				isPlaing = false;
				mtextview.setText("GAME OVER,\n PLACE YOUR BET");
				Log.v(">>>>>>>", "isPlaing = false");
			}
		}, i);
	}
	private void realstopdelay(int i) {
		Handler handler = new Handler();
		handler.postDelayed(new Runnable() {
			public void run() {
				MediaPlayer mp_s = MediaPlayer.create(getBaseContext(), R.raw.reelsstop);
				mp_s.start();
				mp_s.setOnCompletionListener(new OnCompletionListener() {

					@Override
					public void onCompletion(MediaPlayer mp_s) {
						mp_s.release();
					}
				});
			}
		}, i);
	}
}